<template><div><h2 id="快速入门" tabindex="-1"><a class="header-anchor" href="#快速入门"><span>快速入门</span></a></h2>
<p>Laravel 5.5 中文文档 /</p>
<p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-测试-入门指南" tabindex="-1"><a class="header-anchor" href="#laravel-测试-入门指南"><span>Laravel 测试: 入门指南</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#environment">测试环境</a></li>
<li><a href="#creating-and-running-tests">定义并运行测试</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 天生就具有测试的基因。事实上，Laravel 默认就支持用 PHPUnit 来做测试，并为你的应用程序配置好了 <code v-pre>phpunit.xml</code> 文件。框架还提供了一些便利的辅助函数，让你可以更直观的测试应用程序。</p>
<p>默认在你应用的 <code v-pre>tests</code> 目录下包含了两个子目录： <code v-pre>Feature</code> 和 <code v-pre>Unit</code>。单元测试是针对你代码中相对独立而且非常少的一部分代码来进行测试。实际上，大多数单元测试可能都是针对某一个方法来进行的。功能测试是针对你代码中大部分的代码来进行测试，包括几个对象的相互作用，甚至是一个完整的 HTTP 请求 JSON 实例。</p>
<p>在 <code v-pre>Feature</code> 和 <code v-pre>Unit</code> 目录中都有提供一个 <code v-pre>ExampleTest.php</code> 的示例文件。安装新的 Laravel 应用程序之后，只需在命令行上运行 <code v-pre>phpunit</code> 就可以进行测试。</p>
<h2 id="测试环境" tabindex="-1"><a class="header-anchor" href="#测试环境"><span>测试环境</span></a></h2>
<p>在运行测试时，Laravel 会根据 <code v-pre>phpunit.xml</code> 文件中设定好的环境变量自动将环境变量设置为 <code v-pre>testing</code>，并将 Session 及缓存以 <code v-pre>array</code> 的形式存储，也就是说在测试时不会持久化任何 Session 或缓存数据。</p>
<p>你可以随意创建其它必要的测试环境配置。<code v-pre>testing</code> 环境的变量可以在 <code v-pre>phpunit.xml</code> 文件中被修改，但是在运行测试之前，请确保使用 <code v-pre>config:clear</code> Artisan 命令来清除配置信息的缓存。</p>
<h2 id="定义并运行测试" tabindex="-1"><a class="header-anchor" href="#定义并运行测试"><span>定义并运行测试</span></a></h2>
<p>可以使用 <code v-pre>make:test</code> Artisan 命令，创建一个测试用例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 在 Feature 目录下创建一个测试类...</span></span>
<span class="line">php artisan make<span class="token punctuation">:</span>test UserTest</span>
<span class="line"></span>
<span class="line"><span class="token comment">// 在 Unit 目录下创建一个测试类...</span></span>
<span class="line">php artisan make<span class="token punctuation">:</span>test UserTest <span class="token operator">--</span>unit</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>测试类生成之后，你就可以像平常使用 PHPUnit 一样来定义测试方法。要运行测试只需要在终端上运行 <code v-pre>phpunit</code> 命令即可：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Unit</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseTransactions</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 基本的测试用例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">void</span></span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">testBasicTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">assertTrue</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 如果要在你的测试类自定义自己的 <code v-pre>setUp</code> 方法，请确保调用了 <code v-pre>parent::setUp()</code> 方法。</p>
</blockquote>
<hr>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://learnku.com/users/5435" target="_blank" rel="noopener noreferrer">@wqer1019</a></td>
<td><img src="https://avatars3.githubusercontent.com/u/9254545?v=4&amp;s=100" alt="9254545?v=4&amp;s=100"></td>
<td>翻译</td>
<td>laravel是世界上最优雅的框架，<a href="https://github.com/wqer1019" target="_blank" rel="noopener noreferrer">@wqer1019</a> at Github</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


